package com.gutai.admin.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.util.comparator.Comparators;

import com.gutai.admin.dao.IGoodsGalleryDao;
import com.gutai.admin.entity.Goods;
import com.gutai.admin.entity.GoodsGallery;
import com.gutai.admin.service.IGoodsGalleryService;
import com.gutai.admin.service.IGoodsService;

/**
 * 
 * 描述：
 * 
 * <pre>
 * HISTORY
 * ****************************************************************************
 *  ID    DATE            PERSON            REASON
 *  1     2018年11月7日         YC           Create
 * ****************************************************************************
 * </pre>
 * 
 * @author YC
 * @version 1.0
 */
@Service
@CacheConfig(cacheNames = "goodsGallery")
public class GoodsGalleryService implements IGoodsGalleryService {

    @Autowired
    private IGoodsGalleryDao goodsGalleryDao;
    @Autowired
    private IGoodsService goodsService;


    @Override
    @Cacheable(unless = "#result == null || #result.size()==0")
    public List<GoodsGallery> getAll() {
        Sort sort = new Sort(Direction.DESC, "createTime");
        return goodsGalleryDao.findAll(sort);
    }

    @Override
    public List<GoodsGallery> getList(String goodsName) {
//        return goodsGalleryDao.findByGoodsId(goodsId);
        List<Integer> goodsIds = goodsService.findByName(goodsName).stream().map(Goods::getId).collect(Collectors.toList());
        return getAll().stream().filter(item->goodsIds.contains(item.getGoodsId())).collect(Collectors.toList());
    }

    @Override
    @CacheEvict(allEntries = true)
    public void add(GoodsGallery entity) {
        List<GoodsGallery> oldList = findByGoodsId(entity.getGoodsId());
        int maxIndex = oldList.stream().map(GoodsGallery::getSortOrder).max(Comparators.comparable()).orElse(0);
        List<String> imgs = Arrays.asList(entity.getImgUrl().split(","));
        for (String img : imgs) {
            GoodsGallery gallery = new GoodsGallery();
            gallery.setGoodsId(entity.getGoodsId());
            gallery.setImgUrl(img);
            gallery.setSortOrder(++maxIndex);
            goodsGalleryDao.save(gallery);
        }
    }

    @Override
    @CacheEvict(allEntries = true)
    public void update(GoodsGallery entity) {
        goodsGalleryDao.saveAndFlush(entity);
    }

    @Override
    @CacheEvict(allEntries = true)
    public void delete(int id) {
        goodsGalleryDao.deleteById(id);
    }

    @Override
    public GoodsGallery findById(int id) {
        return goodsGalleryDao.findById(id);
    }
    
    @Override
    public List<GoodsGallery> findByGoodsId(int goodsId) {
        return goodsGalleryDao.findByGoodsId(goodsId);
    }

}
